package com.gitee.ice1938.adcode.fun;

import java.io.File;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;

@Component
public class InitCommandLineRunner implements CommandLineRunner {

	@Autowired
	private DictAdministrativeAreaService dictAdministrativeAreaService;

	private enum Headers {
		ad_code, name
	}

	private enum HeadersChange {
		addflag, ad_code, name, startTime
	}

	@Override
	public void run(String... args) throws Exception {

		Calendar cl = Calendar.getInstance();
		int codeModifiedTime = 0;
		// 1980 年使用行政码标准,早期管理不完整,编码会出现重复问题, 或者同一个地区使用不同编码或同一编码显示不同市区
		// 511102,市中区 1988 年的出现记录不全
		// 449001 1991 年删除, 1992年复用
		// 450107 2000 年删除, 2004年复用
		// 152725 120110 更名复用

		String resourcePath = System.getProperty("user.dir");
		int index = resourcePath.lastIndexOf(File.separator);
		resourcePath = resourcePath.substring(0, index)+File.separator +"data"+File.separator;
		for (int year = 1980; year <= cl.get(Calendar.YEAR); year++) {
			if (codeModifiedTime >= year) {
				year = codeModifiedTime;
				continue;
			}
			List<DataRecord> list = new ArrayList<>();
			// Resource res = new ClassPathResource("db/data/" + year + ".csv");
			Resource res = new FileSystemResource(resourcePath + year + ".csv");
			if (res.exists()) {
				Reader csvReader = new InputStreamReader(res.getInputStream(), "UTF-8");
				Iterable<CSVRecord> records = CSVFormat.RFC4180.withHeader(Headers.class).parse(csvReader);
				for (CSVRecord single : records) {
					if (single.getRecordNumber() == 1) {
						continue;
					}
					list.add(DataRecord.form(single));
				}
				//res = new ClassPathResource("db/data/" + year + "_fixadd.csv");
				res = new FileSystemResource(resourcePath + year + "_fixadd.csv");
				if (res.exists()) {
					csvReader = new InputStreamReader(res.getInputStream(), "UTF-8");
					records = CSVFormat.RFC4180.withHeader(Headers.class).parse(csvReader);
					for (CSVRecord single : records) {
						if (single.getRecordNumber() == 1) {
							continue;
						}
						list.add(DataRecord.form(single));
					}
				}
				codeModifiedTime = dictAdministrativeAreaService.importData(list, year, true, "csv");
			}
			//res = new ClassPathResource("db/data/" + year + "_change.csv");
			res = new FileSystemResource(resourcePath + year + "_change.csv");
			if (res.exists()) {
				Reader csvReader = new InputStreamReader(res.getInputStream(), "UTF-8");
				Iterable<CSVRecord> records = CSVFormat.RFC4180.withHeader(HeadersChange.class).parse(csvReader);
				for (CSVRecord single : records) {
					if (single.getRecordNumber() == 1) {
						continue;
					}
					list.add(DataRecord.formChange(single));
				}
				codeModifiedTime = dictAdministrativeAreaService.importData(list, year, false, "csv");
			}
		}
		dictAdministrativeAreaService.queryForCityselect(cl.get(Calendar.YEAR));
	}

}
